package com.mystnihon.freeremote.settings;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.mystnihon.freeremote.config.XmlTag;
import com.mystnihon.freeremote.db.table.SeedboxTable;

public class SettingsParser {

    public static SettingsData parse(String text) throws IOException, SAXException, ParserConfigurationException {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp;
        sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        DataHandler dataHandler = new DataHandler();
        xr.setContentHandler(dataHandler);

        xr.parse(new InputSource(new StringReader(text)));
        return dataHandler.getData();

    }

    public static class DataHandler extends DefaultHandler {

        private final StringBuffer mSb = new StringBuffer();

        private boolean mSeedboxslist = false;
        private SettingsData mData;
        private SeedboxTable seedboxTable;

        public DataHandler() {
            mData = new SettingsData();
        }

        public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
            mSb.setLength(0);

            if (XmlTag.TAG_SEEDBOXS.equals(localName)) {
                mSeedboxslist = true;
            } else if (XmlTag.TAG_SEEDBOX.equals(localName)) {
                if (mSeedboxslist) {
                    seedboxTable = new SeedboxTable();
                } else {
                    mData.refreshTime = Integer.parseInt(atts.getValue(XmlTag.ATTR_REFRESH_TIME));
                    mData.autoStart = Boolean.getBoolean(atts.getValue(XmlTag.ATTR_AUTOSTART));
                }
            } else if (XmlTag.TAG_REMOTE.equals(localName)) {
                mData.remotecode = atts.getValue(XmlTag.ATTR_CODE);
            }

        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (XmlTag.TAG_SEEDBOXS.equals(localName)) {
                mSeedboxslist = false;
            } else if (XmlTag.TAG_SEEDBOX.equals(localName)) {
                if (mSeedboxslist) {
                    mData.seedboxList.add(seedboxTable);
                }
            } else if (XmlTag.TAG_NAME.equals(localName)) {
                seedboxTable.mName = mSb.toString();
            } else if (XmlTag.TAG_HOST.equals(localName)) {
                seedboxTable.mHost = mSb.toString();
            } else if (XmlTag.TAG_LOGIN.equals(localName)) {
                seedboxTable.seedboxLogin = mSb.toString();
            } else if (XmlTag.TAG_PASSWORD.equals(localName)) {
                seedboxTable.seedboxPassword = mSb.toString();
            } else if (XmlTag.TAG_PORT.equals(localName)) {
                seedboxTable.seedboxPort = Integer.parseInt(mSb.toString());
            }
        }

        public void characters(final char[] ch, final int start, final int length) throws SAXException {
            super.characters(ch, start, length);
            mSb.append(ch, start, length);
        }

        public SettingsData getData() {
            return mData;
        }
    }

    public static class SettingsData {
        public ArrayList<SeedboxTable> seedboxList;
        public int refreshTime = 3;
        public boolean autoStart = true;
        public String remotecode = "";

        public SettingsData() {
            seedboxList = new ArrayList<SeedboxTable>();
        }
    }
}
